<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
//分类页控制器
class ListController extends Controller{
    //分类页首页
    public function index(){
    	//标签页
    	$tid = Q('get.tid',0,'intval');
		//处理标签名称
		if($tid){
			//实例化标签表
			$tagModel = new \Common\Model\Tag;
			$name = $tagModel->where("tid={$tid}")->pluck('tname');
			//根据tid筛选文章
			$arcTagModel = new \Common\Model\ArcTag;
			$arcData = $arcTagModel
					->join('article','article_aid','=','aid')
					->join('category','category_cid','=','cid')
					->where("tag_tid={$tid} AND is_recycle=0")
					->get();
//			p($arcData);exit;
		}

    	//分类页
    	$cid = Q('get.cid',0,'intval');
		//处理分类名称
		if($cid){
			//实例化分类表
			$cateModel = new \Common\Model\Cate;
			$name = $cateModel->where("cid={$cid}")->pluck('cname');
			//获得顶级分类、子分类下面筛选出来的文章
			//调取cate模型里的getSon方法(获得子集分类)
			$cids = $cateModel->getSon($cateModel->get(),$cid);
			$cids[] = $cid;
			$arcModel = new \Common\Model\Arc;
			$arcData = $arcModel->join('category','category_cid','=','cid')->where("category_cid in(" . implode(',', $cids) .") AND is_recycle=0")->get();
//			p($arcData);exit;
		}
		//分配名称
		View::with('name',$name);
		
		//把对应标签压入文章数据
		$arcTagModel = new \Common\Model\ArcTag;
		foreach ($arcData as $k => $v) {
			$arcData[$k]['tag']= $arcTagModel->join('tag','tid','=','tag_tid')->where("article_aid={$v['aid']}")->field('tid,tname')->get();
//			p($arcData);exit;
		}
		//分配数据
		View::with('arcData',$arcData);
    	//载入模板
    	View::make();
	}
}